package account.docent.view;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import prototyping.client.AbstractMessage;
import prototyping.client.teacher.model.TeacherClient;

import javax.swing.JButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import dao.server.DataLoadIP;
import account.docent.controller.AfhandelController;
import account.docent.controller.DocentController;
import account.model.SubTeam;
import account.model.HelpListModel;
import account.student.controller.StudentController;

public class DocentView extends JFrame {

	private static final long serialVersionUID = 1L;

	private static DocentView instance;

	private JScrollPane srpHelp = new JScrollPane();
	private JButton btnAfhandelen = new JButton("Afhandelen");
	private HelpListModel hlm = new HelpListModel();
	private JList<AbstractMessage> lstHelp = new JList<AbstractMessage>(hlm);

	private JMenuBar menuBar = new JMenuBar();
	private JMenu mnStart = new JMenu("Start");
	private JMenu mnAccount = new JMenu("Profiel");
	private JMenuItem mntmAanpassen = new JMenuItem("Bewerken");
	private JMenuItem mntmStop = new JMenuItem("Afmelden");
	private JMenuItem menuItem_2 = new JMenuItem("Account beheren");
	private JMenu mnHelp = new JMenu("Mededeling");
	private JMenuItem mntmBewerken = new JMenuItem("Bewerken");
	private JMenu mnHulpvraag = new JMenu("Hulpvraag");
	private JMenuItem mntmBewerken1 = new JMenuItem("Bewerken");

	private String userName;
	private String server;

	public static DocentView getInstance(){
		if(instance == null){
			instance = new DocentView();
		}
		return instance;
	}

	private DocentView() {
		server = DataLoadIP.getInstance().LoadIP();
		userName = SubTeam.getInstance().getAcc().getPersoon().getVoornaam() + " " + SubTeam.getInstance().getAcc().getPersoon().getAchternaam();
		loadServer(userName, server);

		//Load jFrame
		initComponents();
	}

	private void loadServer(String userName, String server){
		//start server connectie
		TeacherClient tc = new TeacherClient(server, userName);
		tc.addObserver(hlm);
		tc.start();	
	}

	private void initComponents() {
		//wachtrij vragen
		lstHelp.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstHelp.setVisibleRowCount(5);
		lstHelp.getAccessibleContext().setAccessibleName("");
		lstHelp.setModel(hlm);
		srpHelp.setViewportView(lstHelp);

		//button handel berichten af
		btnAfhandelen.addActionListener(new AfhandelController(lstHelp, userName, server));
		btnAfhandelen.setActionCommand("Afgehandeld");

		//menu
		setJMenuBar(menuBar);
		menuBar.add(mnStart);
		menuBar.add(mnAccount);
		mnAccount.add(mntmAanpassen);
		
		mntmAanpassen.setActionCommand("AccountOpties");
		mntmAanpassen.addActionListener(new DocentController(mntmAanpassen));
		mntmStop.addActionListener(new DocentController(mntmAanpassen));
		mntmStop.setActionCommand("Afmelden");
		mntmStop.addActionListener(new StudentController(mntmStop));

		mnStart.add(mntmStop);
		menuBar.add(mnHulpvraag);
		mnHulpvraag.add(mntmBewerken1);
		mntmBewerken1.setActionCommand("Hulpvraagknopjes");
		mntmBewerken1.addActionListener(new DocentController(mntmBewerken1));

		menuBar.add(mnHelp);
		mntmBewerken.setActionCommand("MededelingBewerken");
		mntmBewerken.addActionListener(new DocentController(mntmBewerken));
		mnHelp.add(mntmBewerken);

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(srpHelp, GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
								.addComponent(btnAfhandelen, Alignment.TRAILING))
								.addContainerGap())
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(srpHelp, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(btnAfhandelen)
						.addContainerGap())
				);
		getContentPane().setLayout(groupLayout);

		//frame settings
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Welkom " + userName);
		setVisible(true);
		setResizable(false);
		setSize(456, 251);

		//frame start rechtsonder scherm (windows)
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
		Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
		int x = (int) rect.getMaxX() - this.getWidth();
		int y = (int) rect.getMaxY() - this.getHeight();
		this.setLocation(x, y - 45);
	}
}